package com.gkd.store.controllers;



import com.gkd.store.dao.ExperienceRepository;
import com.gkd.store.entities.Experience;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class ExperienceController {

    private ExperienceRepository experienceRepository;

    @Autowired
    public void setExperienceRepository(ExperienceRepository experienceRepository) {
        this.experienceRepository =experienceRepository;
    }

    @RequestMapping(path = "/")
    public String index() {

        return "index";
    }

    @RequestMapping(path = "/experiences/add", method = RequestMethod.GET)
    public String createExperiences(Model model) {

        model.addAttribute("experiences", new ExperienceController());
        return "message";
    }

    @RequestMapping(path = "/experiences", method = RequestMethod.POST)
    public String saveExperience(Experience experience) {

        experienceRepository.save(experience);
        return "redirect:/";
    }

    /*
     *   这个方法的作用是？
     * */
    @RequestMapping(path = "/experiences", method = RequestMethod.GET)
    public String getAllProducts(Model model) {

        model.addAttribute("experiences", experienceRepository.findAll());
        return "experiences";
    }

    /*
     *   这个方法的作用是？
     * */
    @RequestMapping(path = "/experiences/experiences/{id}", method = RequestMethod.GET)
    public String editExperiences(Model model, @PathVariable("id") String id) {

        model.addAttribute("experience", experienceRepository.getById(id));
        return "message";
    }

    /*
     *   这个方法的作用是？
     * */
    @RequestMapping(path = "/experiences/delete/{id}", method = RequestMethod.GET)
    public String deleteExperiences(@PathVariable("id") String id) {

        experienceRepository.deleteById(id);
        return "redirect:/experiences";
    }
}
